% %
% LAYOUT_E.TEX - Short description of REFMAN.CLS
%                                       99-03-20
%
%  Updated for REFMAN.CLS (LaTeX2e)
%
\documentclass[twoside,a4paper]{refart}
\usepackage{makeidx}
\usepackage{ifthen}
\usepackage{amsmath}
\usepackage{graphicx}
% ifthen wird vom Bild von N.Beebe gebraucht!

\def\bs{\char'134 } % backslash in \tt font.
\newcommand{\ie}{i.\,e.,}
\newcommand{\eg}{e.\,g..}
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}

\title{DayTrader -- A Programming Language For Traders\\Language Tutorial}
\author{Tsung-Kai Lin(tl2450)\\
Jie Huang(jh3105)\\
Fang Da(fd2263)\\
Ran Wang(rw2409)\\
Shao-Chuan Wang(sw2644)\\
Department of Computer Science, Columbia University}

\date{}
\emergencystretch1em  %

\pagestyle{myfootings}
\markboth{\textrm{DayTrader} -- A Programming Language For Traders}%
         {\textrm{DayTrader} -- A Programming Language For Traders}

\makeindex 

\setcounter{tocdepth}{2}

\begin{document}

\maketitle


\newpage
\tableofcontents

\newpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Introduction}
DayTrader programming language aims at automatic day trading. 
The goal of DayTrader is to abstract the selection of the target stocks and the discovery of the indicators. 
We make the selection of target stocks and their calculation fairly easy. 
With DayTrader, the user can write just a few lines to complete an auto-trading program.  
\section{Hello World in DayTrader}
Here comes the hello world program in DayTrader:\\\\
1. @Buy\\
2. close / open $>$ 110\%  \hspace*{15pt} // close and open are keywords for close and open price\\
3. buy 100\\
4.\\
5. @Sell\\
6. profit $>$ 10\%\\
7. sell\\

The keyword ``@Buy'' in line 1 indicates the beginning of a buy strategy. 
Line 2 scans all the stocks to see whether they meet the condition that the current price of the stock has grown above 110\% of the day’s open price. 
Line 3 issues a ‘buy’ command to buy 100 units of the first stock which meets the condition defined in line 2. (There may be multiple stocks meeting the condition, but only the first one of them will be bought by default.)\\\\
The keyword ``@Sell'' in line 5 indicates the beginning of a sell strategy. 
Similar to line 2, line 6 scans all the purchased stocks to check whether they meet the condition defined by this line. 
Here, the condition is, whether the profit (if sold right now) is greater than 10\%. 
Line 7 sells all the stocks which meet the condition defined in line 6.\\\\
From the above Hello World example, you can see some features of the language:\\
\begin{enumerate}
\item A lot of work is handled by the language infrastructure, you do not need to write a ‘for’ loop to iterate through all the stocks.
\item You do not even need to write any control flows.
\item There is a rich collection of the handy keywords to deal with frequently used market indicators (like "profit", "close", and "open" in the example)
\end{enumerate}

\section{Programming Model}
%The design of DayTrader language is based on the pipeline model. The model is designed for the need to buy/sell target stocks.
%One is the model of buy and sell engines, which serves as two daemon processes to periodically run user-defined strategy to make decision on buying and selling stocks. The other is the pipeline model, which abstracts the process of selecting the stocks to be traded within a strategy.

\subsection{Pipeline Model}
The pipeline model abstracts the process of selecting a subset of stocks meeting certain conditions.

A typical DayTrader program consists of one "buy" strategy and one "sell" strategy.
Each line of code represents a statement; each statement can be viewed as a filter narrowing down the set of stocks which meet the conditions.

The pipeline model defines an implicit input and output associated with a statement. The output of a statement is the input of next one. Either the input and output contains a boolean value and a set of stocks, illustrated by the following:
\\\\\hspace*{20pt}
\{true $|$ \{MSFT, GOOG, KO, JPM, PG\}\}
\\\\
The boolean value usually indicates the overall logical result of the statement, and the set contains all the stocks left after being filtered by all the previous codes in the strategy. 
If any of the statements of a strategy returns false, the whole strategy returns immediately, and the engine will restart the strategy at the next tick. (So, the input boolean value for a statement is always true.)

The condition defined by a statement is tested against each stock in the input set. 
If any of the conditions are not met for a stock, the stock is removed from the current set, and the current set will be passed to the next statement. 
For example, for the following statement, only the stocks in the input set with profit higher than 10\% will be passed to the next statement.
\\\\\hspace*{20pt}
profit $>$ 10\% 
\\\\
The input boolean value for the first statement of a strategy is always `true'. 
%For the buy strategy, the input set of stocks for the first statement is the set of all the stocks available in the markets. For the sell strategy, the input set is the set of all stocks currently hold in the portfolio.

Users need to understand the pipeline model and keep that in mind when they write the strategy code. 
But for most of the cases, they do not need to directly operate on the pipeline inputs and outputs.

The following snippet shows how the implicit pipleline output might be for our `Hello World' program:

\begin{tabular}{ll}
\text{1. @Buy}&\\
\text{2. select all}&\text{//  \{true $|$ \{all stocks in the market\}\}} \\
\text{3. sector == "Technology"} & \text{//  \{true $|$ \{all stocks in the ‘Technology” sector\}\}}\\
\text{4. MA1(15) crossup MA1(30)}&\text{//  \{true $|$ \{MSFT, GOOG\}\}}\\
&\text{(only MSFT and GOOG meet the condition)} \\
\text{5. buy 100}&\text{//  \{true $|$ \{MSFT, GOOG\}\}}\\\\
\text{6. @Sell}&\\
\text{7. select all}&\text{//  \{true $|$ MSFT, GOOG\}} \\
\text{8. profit $>$ 5\% or lost $>$ 5\%} &\text{//  \{false $|$ \{\}\}  (say, none meets the condition) }\\
\text{9. sell}&
\end{tabular}
\subsection{Buy/Sell Engine Model}

Each DayTrader program consists of two strategies: a buy strategy and a sell strategy, which is denoted by the keyword @Buy and @Sell.

An engine is a daemon process that will run buy strategy and sell strategy interchangeably. 
First, the buy strategy at every market tick until the conditions are met, and a "buy" action is triggered. 
After buy action, the buy strategy is stopped and the sell strategy is instead launched.
The sell strategy is only stopped when all the stocks in the user’s portfolio are sold.
Then, the buy/sell cycle will restart. 
Following is a chart explaining the buy and sell cycles:\\
\begin{figure}[h!]
\centering
\includegraphics[scale=0.45]{pic}
\caption{Programming Model of DayTrader}
\end{figure}

This design is based on the nature of day trading. Stocks purchased are usually sold in just several minutes or even seconds. 
If the conditions of the sell strategy has never been met by the market close time of the day, the stocks are then forcefully sold out by the system.


\section{Keyword and Identifier}
\subsection{Select Keyword}
In day trading system, sometimes users want to focus on certain stocks instead of the whole market. 
Using "select" as a set function can help you focus on the certain stock.\\\\
Example:\\
\hspace*{20pt}select "MSFT"  // focus on Microsoft in the stock market
\subsection{Domain Specific Keyword}
In day trading system, it’s helpful to find a certain stock to operate by some attribute of the stock or market. 
Most strategy focuses on these attribute to select a better stock.
Some common information such as the close, volume or sector is stated as follows. \\\\
Example:\\
\hspace*{20pt}sector == "Consumer"  // focus on consumer companies stock\\
\hspace*{20pt}close $<$ 100     // focus on the stock with the close price smaller that 100\\
\subsection{Identifier}
DayTrader provides common indicator for users to find the stock we are looking for. 
A common identifier is the MA1(\textbf{int} days), which is the moving average of given time. 
The change in certain stock is fast, and using the identifier, we can get the average of a given time. 
The MA1(15) crossup MA1(30) indicate the two change of stock intersect, and MA1(15) cross up MA1(30). 
This information gives user a sign to buy the stock.\\\\
Example combine function with Identifier:\\
\hspace*{20pt}MA1(15) crossup MA1(30) //focus on the stock which meets this condition
\subsection{Action}
When certain stocks meet previous condition, trader may want to perform buy/sell action to those stocks.
A action statement can define the strategy to perform the action.\\\\
Example:\\
\hspace*{20pt}buy 10 \hspace*{20pt}// buy 10 for stocks which meet previous condition\\
\hspace*{20pt}sell \hspace*{20pt}// sell all stocks which meet previous condition
\subsection{Comments}
// This is a comment.
\section{How to Write a DayTrader Program}
Let’s look at another example which is more complex and hopefully more usefully in the real world:\\\\
1.  @Buy\\
2.  select all \hspace*{100pt}                // "select all" is default setting\\
3.  sector == "Technology"\hspace*{30pt}    // consider stocks only in the ‘Technology’ sector\\
4.  Math.round(volume) $>$ Property.getd("MSFT", "volume")\\
5.  print volume\\
6.  MA1(15) crossup MA1(30)\\
7.  buy 100\\
8. \\
9.  @Sell\\
10. profit $>$ 5\% or lost $>$ 2\%\\                   
11. sell \\\\

Line 4 means that if a stock in scope (after being filtered by the previous lines) has a trading volume greater than today’s trading volume of ‘MSFT’ (Microsoft), then keep it in the set of stocks for possible buying, otherwise, discard it.

Line 5 means print the current volume value.

In line 6, "crossup" is a trading term which every traders must be familiar with. 
The line reads like this: If the 15-day moving average cross up the 30-day moving average, then keep it. 

Line 10 defines the condition to sell: the profit is greater than 5 percent or the loss is great than 2 percent. 
Then, line 11 indicates that if the condition defined in line 10 is met, sell 50\% of the units of stock purchased.

\printindex

\end{document}
